package control
{
	import flash.net.URLVariables;
	
	import model.ModelLocator;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class findPath
	{
		
		internal var theModel:ModelLocator = ModelLocator.getInstance();
		private var findPathByKeyHttp:HTTPService;
		private var findPathByUserHttp:HTTPService;
		public function findPath()
		{
			findPathByKeyHttp = new HTTPService();
			findPathByKeyHttp.url = "http://localhost:8080/GMdb/servlet/findPathByKey";
			findPathByKeyHttp.addEventListener(ResultEvent.RESULT,ResultHandler);
			findPathByKeyHttp.addEventListener(FaultEvent.FAULT, fHandler);
			findPathByUserHttp = new HTTPService();
			findPathByUserHttp.url = "http://localhost:8080/GMdb/servlet/findPathByUser";
			findPathByUserHttp.addEventListener(ResultEvent.RESULT,ResultHandler);
			findPathByUserHttp.addEventListener(FaultEvent.FAULT, fHandler);
		}
		public function find(key:String):void{
			//Alert.show(pathName);
			
			var params:URLVariables = new URLVariables();
       		params.key = key;
            findPathByKeyHttp.send(params);
            
		}
		public function findByUser(creater:String):void{
			var params:URLVariables = new URLVariables();
       		params.creater = creater;
            findPathByUserHttp.send(params);
		}
		internal function ResultHandler(event:ResultEvent):void {
         	theModel.allPlans=event.result.Result.Plan as ArrayCollection;
        }
        
		internal function fHandler (event:FaultEvent):void {
         		Alert.show(event.fault.message,"Error")
        }

	}
}